package com.example.t1.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.example.t1.Entity.DatabaseConnection;

public class ResourceHandler {
	
	public static final int TYPE_USERPROFILE = 1;
	public static final int TYPE_ACCOUNT = 2;
	public static final int TYPE_ADMIN_FUNCTION = 3;
	public static final int TYPE_USER_FUNCTION = 4;
	
	private static final String USERPROFILE = "src/properties/UserProfile.xml";
    private static final String USERPROFILE_SQL = "SELECT * FROM CUSTOMERSINFO WHERE LOGINUSERNAME = '%s';";
    private static final String ACCOUNT = "src/properties/Accounts.xml";
    private static final String ACCOUNT_SQL = " ";
    private static final String ADMIN_FUNCTION = "src/properties/adminFunction.xml";
    private static final String USER_FUNCTION = "src/properties/userFunction.xml";

    public static void createProperties(int type, String username) {
        String filePath = getFilename(type);
        String sql = String.format(getSQL(type), username);
        
        File file = new File(filePath);
        if (file.exists())
            file.delete();
        Properties pros = new Properties();
        DatabaseConnection db = new DatabaseConnection();
        ResultSet rs = db.executeQuery(sql);
        try {
            ResultSetMetaData metadata = rs.getMetaData();
            rs.next();
            for (int i = 1; i <= metadata.getColumnCount(); i++) {
                pros.setProperty(metadata.getColumnLabel(i), rs.getString(i));
            }
            FileOutputStream fout = new FileOutputStream(filePath);
            pros.storeToXML(fout, null);
        } catch (SQLException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        db.closeConnection();
        String id = getValue(TYPE_USERPROFILE, "CUSTOMERID");
        String role = getValue(TYPE_USERPROFILE, "Role");
        Session.addAttribute("id", id);
        Session.addAttribute("role", role);
    }

    private static String getSQL(int type) {
    	switch (type) {
    	case TYPE_USERPROFILE:
    		return USERPROFILE_SQL;
    	case TYPE_ACCOUNT:
    		return ACCOUNT_SQL;
    	default:
    		return null;
    	}
    }

    private static String getFilename(int type) {
    	switch (type) {
    	case TYPE_USERPROFILE:
    		return USERPROFILE;
    	case TYPE_ACCOUNT:
    		return ACCOUNT;
    	case TYPE_ADMIN_FUNCTION:
    		return ADMIN_FUNCTION;
    	case TYPE_USER_FUNCTION:
    		return USER_FUNCTION;
    	default:
    		return null;
    	}
    }

    private static Properties getPropertiesFile(int type) {
        try {
            File file = new File(getFilename(type));
            Properties pros = new Properties();
            FileInputStream ins = new FileInputStream(file);
            pros.loadFromXML(ins);
            return pros;
        } catch (InvalidPropertiesFormatException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (IOException ex) {
            Logger.getLogger(ResourceHandler.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static String getValue(int type, String key) {
        return getPropertiesFile(type).getProperty(key);
    }

    public static Map<String, String> getAllValue(int type) {
        Properties pros = getPropertiesFile(type);
        Map<String, String> result = new LinkedHashMap<String, String>();
        for (Map.Entry<Object, Object> map : pros.entrySet()) {
            result.put((String) map.getKey(), (String) map.getValue());
        }
        return result;
    }

    public static void delete() {
        new File(USERPROFILE).delete();
    }
}
